/*
 * @Description:
 * @Version: 2.0
 * @Autor: Seven
 * @Date: 2023-10-19 16:08:00
 * @LastEditors: Seven
 * @LastEditTime: 2023-10-27 17:15:31
 */
import { getTroubleAdd, getTroubleEdit } from "@/services/modules/plan";
import { Form, Input, Modal, Select, message } from "antd";
import React, { memo, useEffect, useRef } from "react";
import { loacalCache } from "@/ultils/func";
const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 6 },
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 18 },
  },
};
const { Option } = Select;
const AddModal = memo(
  ({ modalVisible, handleCancel, getList, title = "新增", rowInfoData }) => {
    const formRef = useRef();
    const [form] = Form.useForm();
    function onFinish() {
      form
        .validateFields({ validateOnly: true })
        .then(() => {
          let data = form.getFieldsValue();
          if (title === "编辑") {
            getTroubleEdit(rowInfoData.id, data).then((res) => {
              if (res.code === 200) {
                getList();
                handleCancel();
              } else {
                message.warning(res?.msg);
              }
            });
          } else {
            getTroubleAdd(data).then((res) => {
              if (res.code === 200) {
                getList();
                handleCancel();
              } else {
                message.warning(res?.msg);
              }
            });
          }
        })
        .catch((err) => err);
    }

    useEffect(() => {
      if (title === "编辑") {
        form.setFieldsValue(rowInfoData);
      }
    }, [rowInfoData, form, title]);

    return (
      <div>
        <Modal
          title={title}
          open={modalVisible}
          onOk={onFinish}
          onCancel={handleCancel}
        >
          <Form
            {...formItemLayout}
            ref={formRef}
            onFinish={onFinish}
            form={form}
            style={{
              maxWidth: 600,
            }}
          >
            {title !== "编辑" && (
              <Form.Item
                name="maintenanceId"
                label="检修工单编号"
                rules={[
                  {
                    required: true,
                    message: "请输入你的检修工单编号",
                  },
                ]}
              >
                <Input />
              </Form.Item>
            )}
            <Form.Item
              name="maintenanceDesc"
              label="检修工单描述"
              rules={[
                {
                  required: true,
                  message: "请输入你的检修工单描述",
                },
              ]}
            >
              <Input />
            </Form.Item>
            <Form.Item
              name="speciality"
              label="专业"
              rules={[
                {
                  required: true,
                  message: "请输入你的专业",
                },
              ]}
            >
              <Select>
                {loacalCache.getCache("DicDataList")?.["falut_speciality"] &&
                  loacalCache
                    .getCache("DicDataList")
                    ?.["falut_speciality"].map((item) => {
                      return (
                        <Option value={item.value} key={item.id}>
                          {item.label}
                        </Option>
                      );
                    })}
              </Select>
            </Form.Item>
            <Form.Item
              name="line"
              label="线路"
              rules={[
                {
                  required: true,
                  message: "请输入你的线路",
                },
              ]}
            >
              <Select>
                {loacalCache.getCache("DicDataList")?.["line"] &&
                  loacalCache.getCache("DicDataList")?.["line"].map((item) => {
                    return (
                      <Option value={item.value} key={item.id}>
                        {item.label}
                      </Option>
                    );
                  })}
              </Select>
            </Form.Item>
            {title !== "编辑" && (
              <Form.Item
                name="status"
                label="状态"
                rules={[
                  {
                    required: true,
                    message: "请输入你的状态",
                  },
                ]}
              >
                <Select>
                  {loacalCache.getCache("DicDataList")?.["trouble_status"] &&
                    loacalCache
                      .getCache("DicDataList")
                      ?.["trouble_status"].map((item) => {
                        return (
                          <Option value={item.value} key={item.id}>
                            {item.label}
                          </Option>
                        );
                      })}
                </Select>
              </Form.Item>
            )}
            <Form.Item
              name="dept"
              label="部门"
              rules={[
                {
                  required: true,
                  message: "请输入你的部门",
                },
              ]}
            >
              <Input />
            </Form.Item>
            <Form.Item
              name="workshop"
              label="车间"
              rules={[
                {
                  required: true,
                  message: "请输入你的车间",
                },
              ]}
            >
              <Select>
                {loacalCache.getCache("DicDataList")["workshop"] &&
                  loacalCache
                    .getCache("DicDataList")
                    ["workshop"].map((item) => {
                      return (
                        <Option value={item.value} key={item.id}>
                          {item.label}
                        </Option>
                      );
                    })}
              </Select>
            </Form.Item>
            <Form.Item
              name="team"
              label="班组"
              rules={[
                {
                  required: true,
                  message: "请输入你的班组",
                },
              ]}
            >
              <Input />
            </Form.Item>
            {title !== "编辑" && (
              <Form.Item name="isFinish" label="是否完成">
                <Select>
                  <Option value={0} key={0}>
                    未完成
                  </Option>
                  <Option value={1} key={1}>
                    已完成
                  </Option>
                </Select>
              </Form.Item>
            )}
          </Form>
        </Modal>
      </div>
    );
  }
);

export default AddModal;
